package com.thinvent.smart.chain.controller;

import com.alipay.mychain.sdk.message.transaction.contract.UpdateContractRequest;
import com.thinvent.smart.chain.contract.AntChainDemo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/bank")
@RestController
public class BankController {

    /**
     *
     * 初始化银行卡
     * @param accountId
     * @return
     * @throws IOException
     */
    @RequestMapping("/setBankAccount")
    public String setBankAccount(@RequestParam("accountId") String accountId) throws IOException {
        AntChainDemo.setBankAccount(accountId);
        return "success";
    }

    /**
     * 获取银行卡余额
     * @param accountId
     * @return
     * @throws IOException
     */
    @RequestMapping("/getBankAccount")
    public Integer getBankAccount(@RequestParam("accountId") String accountId) throws IOException {
        return AntChainDemo.getBankAccount(accountId);
    }

    /**
     * 存钱
     * @param accountId
     * @param amount
     * @return
     * @throws IOException
     */
    @RequestMapping("/save")
    public String save(@RequestParam("accountId") String accountId, @RequestParam("amount") Integer amount) throws IOException {
        return AntChainDemo.save(accountId, amount);
    }

    /**
     * 取钱
     * @param accountId
     * @param amount
     * @return
     * @throws IOException
     */
    @RequestMapping("/withDraw")
    public String withDraw(@RequestParam("accountId") String accountId, @RequestParam("amount") Integer amount) throws IOException {
        return AntChainDemo.withDraw(accountId, amount);
    }

    /**
     * 测试更新合约
     * @return
     * @throws IOException
     */
    @RequestMapping("/test")
    public String test() throws IOException {
        return AntChainDemo.test();
    }

    /**
     * 更新合约
     * @return
     * @throws IOException
     */
    @RequestMapping("/deploy")
    public String update() throws IOException {
        AntChainDemo.deployContract();
        return "success";
    }

    /**
     * 测试合约升级新方法
     * @return
     */
    @RequestMapping("/testUpdate")
    public String testUpdate() throws IOException {
        return AntChainDemo.test();
    }
}
